package com.raven.play.stream.test.javadsl.tutorial;

import akka.Done;
import akka.NotUsed;
import akka.actor.typed.ActorSystem;
import akka.actor.typed.javadsl.Behaviors;
import akka.japi.Pair;
import akka.stream.Materializer;
import akka.stream.OverflowStrategy;
import akka.stream.javadsl.*;
import com.raven.play.stream.test.BaseTest;
import org.junit.jupiter.api.Test;

import java.util.concurrent.CompletionStage;

public class $06MaterializingStreams extends BaseTest {

    @Test
    public void test(){

        Source<Integer, NotUsed> source = Source.range(1, 10);
        // Source<Integer, NotUsed> source = Source.from(Arrays.asList(1, 2, 3, 4, 5));
        Sink<Integer, CompletionStage<Done>> sink = Sink.foreach(it -> log.info(it.toString()));

        Sink<Integer, CompletionStage<String>> fold = Sink.fold("", (String arg1, Integer arg2) -> arg1 + arg2);


        /**
         * toMat 第二个参数是选择 Source 或 当前算子的 Materialized 值, 所以会有对应的 left 和 right, 简化使用 Keep 来代替
         * Materialize 时,
         *      left 是 NotUsed 是由于 source 没有产生 Materialized 值
         *      right 产生的 Materialized 值是 CompletionStage<Done>
         */
        CompletionStage<Done> run1 = source.toMat(sink, Keep.right()).run(system);
        NotUsed run2 = source.toMat(sink, (NotUsed left, CompletionStage<Done> right) -> left).run(system);
        NotUsed run3 = source.toMat(sink, Keep.left()).run(system);
        Pair<NotUsed, CompletionStage<Done>> run4 = source.toMat(sink, Keep.both()).run(system);
        NotUsed run5 = source.toMat(sink, Keep.none()).run(system);

        Pair<NotUsed, CompletionStage<String>> run7 = source.toMat(fold, Keep.both()).run(system);

        // FIXME: 2023-12-26 10:00:41 为什么这里的 Materialized 值 还是 NotUsed ?
        Source<Integer, SourceQueueWithComplete<Integer>> sourceWithMat =
                Source.<Integer>queue(10, OverflowStrategy.dropNew());
        Pair<NotUsed, CompletionStage<String>> run8 = source.toMat(fold, Keep.both()).run(system);

        // FIXME: 2023-12-26 11:38:03 Materialized 值究竟放什么? 怎么使用?
    }
}
